package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
//跟前端进行交互的 接受请求响应数据的controller层
public class DeptController {
    //通过任务派发 也就是逻辑业务任务接口的对象去执行它的实现类对象
    //使用多态
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result selectAllDept() {
        log.info("正在查询所有部门.........");
        List<Dept> depts = deptService.selectAllDept();
        return Result.success(depts);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        log.info("正在根据id查询部门..................");
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    @PostMapping
    public Result insertDept(@RequestBody Dept dept) {
        log.info("正在添加部门...........dept={}", dept);
        deptService.insertDept(dept);
        return Result.success();
    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        log.info("正在修改部门............dept={}", dept);
        deptService.updateDept(dept);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id) {
        deptService.deleteDept(id);
        return Result.success();
    }
}
